<template>
  <div>
    <Category :scene="scene" />

    <el-card style="margin-top: 10px">
      <!-- card body -->
      <div v-show="scene === 0">
        <el-button v-has="'btn.Spu.add'" :disabled="categoryStore.c3Id ? false : true" type="primary" size="default" icon="Plus" @click="addSpu">添加SPU</el-button>
        <el-table :data="records" border stripe style="margin: 10px 0px">
          <el-table-column type="index" label="序号" width="80px" />
          <el-table-column label="SPU名称">
            <template #default="{ row }">{{ row.spuName }}</template>
          </el-table-column>
          <el-table-column label="SPU描述" show-overflow-tooltip>
            <template #default="{ row }">{{ row.description }}</template>
          </el-table-column>
          <el-table-column label="SPU操作">
            <template #default="{ row }">
              <el-button v-has="'btn.Spu.addsku'" title="添加SKU" type="primary" size="default" icon="Plus" @click="addSku(row)" />
              <el-button v-has="'btn.Spu.update'" title="修改SPU" type="warning" size="default" icon="Edit" @click="updateSpu(row)" />
              <el-button v-has="'btn.Spu.skus'" title="查看SKU列表" type="info" size="default" icon="View" @click="findSku(row)" />
              <el-popconfirm :title="`是否要删除${row.spuName}`" width="200px" @confirm="removeSpu(row)">
                <template #reference>
                  <el-button v-has="'btn.Spu.delete'" title="删除SPU" type="danger" size="default" icon="Delete" />
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>

        <el-pagination v-model:page-size="pageSize" v-model:current-page="pageNo" class="pagination" :page-sizes="[3, 5, 7, 9]" size="large" :background="true" layout="prev, pager, next, jumper,->, sizes,total" :total="total" @change="getHasSpu" />
      </div>
      <spu-form v-show="scene === 1" ref="spu" @changeScene="changeScene" />
      <sku-form v-show="scene === 2" ref="sku" @changeScene="changeScene" />

      <el-dialog v-model="show" title="SKU列表" width="30%" @close="">
        <el-table :data="skuArr" border stripe>
          <el-table-column label="SKU名字" prop="skuName" />
          <el-table-column label="SKU价格" prop="price" />
          <el-table-column label="SKU重量" prop="weight" />
          <el-table-column label="SKU名图片">
            <template #default="{ row }">
              <img :src="row.skuDefaultImg" style="height: 100px; width: 100px" />
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { onBeforeMount, ref, watch } from 'vue';
import useCategoryStore from '@/store/modules/category';
import SpuForm from './spuForm.vue';
import SkuForm from './skuForm.vue';

import { reqHasSpu, reqSkuList, reqRemoveSpu } from '@/api/product/spu';

import type { SkuInfoData, HasSpuResponseData, Records, SpuData, SkuData } from '@/api/product/spu/type';

import { ElMessage } from 'element-plus';

let categoryStore = useCategoryStore();

watch(
  () => categoryStore.c3Id,
  () => {
    if (!categoryStore.c3Id) return;
    getHasSpu();
  },
);
//场景切换
let scene = ref<number>(0);

// 页面数据数量
let pageSize = ref<number>(3);

//上次页面数据数量
let lastpageSize = ref<number>(3);

// 数据总量
let total = ref<number>(0);
//当前页数
let pageNo = ref<number>(1);
//存储SPU列表
let records = ref<Records>([]);

// 节流阀
let loading = ref<boolean>(false);
//获取三级分类下的SPU
const getHasSpu = async () => {
  if (loading.value) return;
  loading.value = true;
  if (pageSize.value !== lastpageSize.value) {
    pageNo.value = 1;
    lastpageSize.value = pageSize.value;
  }
  let result: HasSpuResponseData = await reqHasSpu(pageNo.value, pageSize.value, categoryStore.c3Id);
  if (result.code === 200) {
    records.value = result.data.records;
    total.value = result.data.total;
  }
  loading.value = false;
};

//卸载页面前清空分类选择数据
onBeforeMount(() => {
  categoryStore.$reset();
});

//spu组件示例
const spu = ref();

// scene切换为1，添加SPU界面
const addSpu = () => {
  scene.value = 1;
  spu.value.initAddSpu(categoryStore.c3Id);
};

// scene切换为1，修改SPU界面
const updateSpu = (row: SpuData) => {
  scene.value = 1;
  spu.value.initHasSpuData(row);
};

type changeSceneParams = {
  flag: number;
  params: string;
};
//scene切换为0，子组件切换至父组件场景
const changeScene = ({ flag, params }: changeSceneParams) => {
  scene.value = flag;
  if (flag === 0) {
    if (params === 'add') {
      pageNo.value = 1;
      getHasSpu();
    } else if (params === 'update') {
      getHasSpu();
    }
  }
};

//sku示例
let sku = ref();

// 添加sku
const addSku = (row: SpuData) => {
  scene.value = 2;
  sku.value.initSkuData(categoryStore.c1Id, categoryStore.c2Id, row);
};

//存储sku列表
let skuArr = ref<SkuData[]>([]);

let show = ref<boolean>(false);
// 查看sku列表
const findSku = async (row: SpuData) => {
  let result: SkuInfoData = await reqSkuList(row.id as number);
  if (result.code === 200) {
    skuArr.value = result.data;
    show.value = true;
  }
};

//删除SPU
const removeSpu = async (row: SpuData) => {
  let result = await reqRemoveSpu(row.id as number);
  if (result.code === 200) {
    ElMessage({
      type: 'success',
      message: '删除成功',
    });
    if (records.value.length === 1 && pageNo.value !== 1) {
      pageNo.value -= 1;
    }
    getHasSpu();
  } else {
    ElMessage({
      type: 'error',
      message: '删除失败',
    });
  }
};
</script>

<style lang="scss" scoped></style>
